home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games: Greatest Hits 1996 / Amiga Games: Greatest Hits 1996.iso / archive / userbox / publicdomain / engclock_v7.0.lha / EngClock_v7.0 / EngClock7_Source / tile.c < prev    next >
C/C++ Source or Header  |  1995-12-05  |  12KB  |  437 lines

  1. /* Tile v1.0 */
  2. /* Written in 1995 by Ben Matthew */
  3.  
  4. /* This code is based on the public domain program ViewDT which was written
  5. by Cloanto , Inc - writers of Personal Paint.  I do not claim to have
  6. written any of the code specifically dealing with the reading of the
  7. datatypes and the images used in the backdrop.  Most of the code in ViewDT
  8. has been left alone - I have adapted the code in several major ways when
  9. dealing with the displaying of the images.  */
  10.  
  11. /* Because of the nature of this code, none of the below is copyrighted
  12. in any way and is submitted to the public domain in all senses. */
  13.  
  14. /* Note that we do not pull in proto/datatpes.h because I do not want
  15. to restrict this program to OS 2.1+ users - if proto/#? are included then
  16. SAS will auto open libs at the start.  This way backdrops are not 
  17. available to older Amigas but the rest of the program will work with
  18. OS 2.04 machines! */
  19.  
  20.  
  21. //#include <proto/datatypes.h>  /* Old system */
  22. #include <clib/datatypes_protos.h>
  23. #include <pragmas/datatypes_pragmas.h>
  24. //#include <proto/exec.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. //#include <proto/intuition.h>
  29. #include <intuition/intuition.h>
  30. #include <datatypes/datatypes.h>
  31. #include <datatypes/pictureclass.h>
  32. #include <exec/exec.h>
  33. #include <dos/dosasl.h>
  34. #include <clib/dos_protos.h>
  35. #include <clib/graphics_protos.h>
  36.  
  37. #include <clib/exec_protos.h>
  38. #include <clib/intuition_protos.h>
  39. #include <clib/datatypes_protos.h>
  40.  
  41. extern void msg(char *msg);
  42.  
  43. extern struct Library *DataTypesBase;
  44. extern struct IntuitionBase *IntuitionBase;
  45. extern struct GfxBase *GfxBase;
  46. extern struct Library *UtilityBase;
  47. extern struct Library *GadToolsBase;
  48. extern struct Library *DiskfontBase;
  49. extern struct Library *AslBase;
  50. extern struct Library *IFFParseBase;
  51. extern struct Library *DOSBase;
  52.  
  53.  
  54. struct Picture
  55. {
  56.    struct BitMapHeader bmhd;  /* format and infos */
  57.    struct BitMap *bmap;       /* bitmap */
  58.    ULONG *palette;            /* color table in LoadRGB32() format */
  59.    LONG palette_size;         /* mem usage */
  60.    LONG palette_entries;      /* number of colors */
  61.    ULONG display_ID;          /* video mode */
  62.    UBYTE *author;             /* author info */
  63.    UBYTE *copyright;          /* copyright info */
  64.    UBYTE *annotation;         /* other info */
  65.    LONG author_size;          /* mem usage */
  66.    LONG copyright_size;       /* mem usage */
  67.    LONG annotation_size;      /* mem usage */
  68. };
  69.  
  70. extern struct Picture piccy;
  71.  
  72. struct AnchorPath *ap;
  73.  
  74. void FreePicture(struct Picture *pic);
  75. LONG GetDataTypesPicture(UBYTE *file_name, struct Picture *pic, ULONG, struct Screen *scr);
  76. BOOL IsDataTypes(UBYTE *file_name, UBYTE *name_buff, LONG nbuff_size);
  77. BOOL ViewPicture(struct Picture *pic, struct Window *win, struct Screen *scr, int x, int y);
  78. BOOL loadpic(char *fname, struct Screen *scr);
  79. void pattern(struct Window *win_p, struct Screen *scr,struct Picture pic);
  80. void unloadpic(struct Picture pic);
  81.  
  82.  
  83. /*
  84.    IsDataTypes
  85.  
  86.    Parameters
  87.       file_name: name of the file to inspect
  88.       name_buff: (optional) buffer to store the file format name
  89.       nbuff_size: size of name_buff
  90.  
  91.    Return value
  92.       TRUE if DataTypes recognized the file as a valid picture file
  93.       FALSE otherwise
  94. */
  95. BOOL IsDataTypes(UBYTE *file_name, UBYTE *name_buff, LONG nbuff_size)
  96. {
  97.    struct DataType *dtn;
  98.    struct DataTypeHeader *dth;
  99.    BPTR lock;
  100.    BOOL it_is;
  101.  
  102.    it_is = FALSE;
  103.    if (lock = Lock(file_name, ACCESS_READ))
  104.    {
  105.       /* inspect file */
  106.       if (dtn = ObtainDataTypeA(DTST_FILE, (APTR)lock, NULL))
  107.       {
  108.          dth = dtn->dtn_Header;
  109.          if (dth->dth_GroupID == GID_PICTURE)   /* is it a picture? */
  110.          {
  111.             it_is = TRUE;
  112.             if (name_buff)
  113.             {
  114.                strncpy(name_buff, dth->dth_Name, nbuff_size);
  115.                *(name_buff + nbuff_size - 1) = 0;  /* safe strncpy() termination */
  116.             }
  117.          }
  118.          ReleaseDataType(dtn);
  119.       }
  120.       UnLock(lock);
  121.    }
  122.    return(it_is);
  123. }
  124.  
  125. /*
  126.    GetDataTypesPicture
  127.  
  128.    Parameters
  129.       file_name: name of the file to load
  130.       pic: work structure
  131.       bmap_flags: AllocBitMap() flags (BMF_DISPLAYABLE, BMF_INTERLEAVED etc.)
  132.  
  133.    Return value
  134.       0 if successful (picture info and data in "pic" structure) or
  135.       error code as from dos.library IoErr() function
  136. */
  137. LONG GetDataTypesPicture(UBYTE *file_name, struct Picture *pic, ULONG bmap_flags, struct Screen *scr)
  138. {
  139.    Object *obj;
  140.    struct BitMapHeader *bmh;
  141.    struct BitMap *bmap;
  142.    struct gpLayout layout;
  143.    ULONG *creg, *ctab;
  144.    UBYTE *str;
  145.    LONG ncol, crsize, err;
  146.  
  147.    memset(pic, 0, sizeof(struct Picture));   /* clear pic structure */
  148.    err = 0;
  149.  
  150.    if (obj = NewDTObject(file_name,
  151.                          DTA_SourceType, DTST_FILE,
  152.                          DTA_GroupID, GID_PICTURE,
  153.              PDTA_Remap, TRUE,
  154.              PDTA_Screen, scr,
  155.                          TAG_DONE))    /* get the picture object */
  156.    {
  157.       
  158.       if (GetDTAttrs(obj,
  159.                      PDTA_ModeID, &pic->display_ID,
  160.                      PDTA_BitMapHeader, &bmh,
  161.                      TAG_DONE) == 2)   /* get the bitmap_header and mode_id */
  162.       {
  163.          pic->bmhd = *bmh;
  164.  
  165.          /*
  166.             query the object about its author, copyright and annotation
  167.          */
  168.          if (GetDTAttrs(obj, DTA_ObjAuthor, &str, TAG_DONE) == 1)
  169.          {
  170.             if (str)
  171.             {
  172.                pic->author_size = strlen(str) + 1;
  173.                if (pic->author = AllocMem(pic->author_size, 0))
  174.                   strcpy(pic->author, str);
  175.             }
  176.          }
  177.          if (GetDTAttrs(obj, DTA_ObjCopyright, &str, TAG_DONE) == 1)
  178.          {
  179.             if (str)
  180.             {
  181.                pic->copyright_size = strlen(str) + 1;
  182.                if (pic->copyright = AllocMem(pic->copyright_size, 0))
  183.                   strcpy(pic->copyright, str);
  184.             }
  185.          }
  186.          if (GetDTAttrs(obj, DTA_ObjAnnotation, &str, TAG_DONE) == 1)
  187.          {
  188.             if (str)
  189.             {
  190.                pic->annotation_size = strlen(str) + 1;
  191.                if (pic->annotation = AllocMem(pic->annotation_size, 0))
  192.                   strcpy(pic->annotation, str);
  193.             }
  194.          }
  195.  
  196.          layout.MethodID = DTM_PROCLAYOUT;   /* render the object */
  197.          layout.gpl_GInfo = NULL;
  198.          layout.gpl_Initial = TRUE;
  199.  
  200.          if (DoDTMethodA(obj, NULL, NULL, (Msg)&layout))
  201.          {
  202.             if (GetDTAttrs(obj,
  203.                            PDTA_DestBitMap, &bmap,
  204.                            PDTA_CRegs, &creg,
  205.                            PDTA_NumColors, &ncol,
  206.                            TAG_DONE) == 3)   /* get the bitmap and its colors */
  207.             {
  208.                if (bmap != NULL && creg != NULL && ncol != 0)
  209.                {
  210.                   crsize = (ncol * 3) * 4;
  211.                   pic->palette_entries = ncol;
  212.                   pic->palette_size = crsize + (2 * 4);  /* LoadRGB32() table requirements */
  213.  
  214.                   if (pic->palette = AllocMem(pic->palette_size, 0))
  215.                   {
  216.                      ctab = pic->palette;
  217.                      *ctab++ = (ncol << 16) | 0;   /* number of colors and first color to load */
  218.                      memcpy(ctab, creg, crsize);
  219.                      *(ctab + (crsize / 4)) = 0;   /* terminator */
  220.                   }
  221.                   else err = ERROR_NO_FREE_STORE;
  222.  
  223.                   if (pic->bmap = AllocBitMap(pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, pic->bmhd.bmh_Depth, bmap_flags, bmap))
  224.                   {
  225.                      BltBitMap(bmap, 0,0, pic->bmap, 0,0, pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, 0xC0, 0xFF, NULL);
  226.                      WaitBlit();
  227.                   }
  228.                   else err = ERROR_NO_FREE_STORE;
  229.                }
  230.                else err = ERROR_REQUIRED_ARG_MISSING;
  231.             }
  232.             else err = IoErr();
  233.          }
  234.          else err = IoErr();
  235.       }
  236.       else err = ERROR_REQUIRED_ARG_MISSING;
  237.  
  238.       DisposeDTObject(obj);   /* free the object */
  239.    }
  240.    else err = IoErr();
  241.  
  242.    if (err)
  243.       FreePicture(pic);
  244.  
  245.    return(err);
  246. }
  247.  
  248. /*
  249.    FreePicture
  250.  
  251.    Parameters
  252.       pic: Picture structure with resources to free
  253.  
  254.    Return value
  255.       none
  256. */
  257. void FreePicture(struct Picture *pic)
  258. {
  259.    if (pic->bmap)
  260.    {
  261.       WaitBlit();
  262.       FreeBitMap(pic->bmap);
  263.    }
  264.    if (pic->palette)
  265.       FreeMem(pic->palette, pic->palette_size);
  266.  
  267.    if (pic->author)
  268.       FreeMem(pic->author, pic->author_size);
  269.  
  270.    if (pic->copyright)
  271.       FreeMem(pic->copyright, pic->copyright_size);
  272.  
  273.    if (pic->annotation)
  274.       FreeMem(pic->annotation, pic->annotation_size);
  275.  
  276.    memset(pic, 0, sizeof(struct Picture));   /* clear it all */
  277. }
  278.  
  279. /*
  280.    ViewPicture
  281.  
  282.    Parameters
  283.       pic: picture infos and data
  284.  
  285.    Return value
  286.       TRUE if the user cancelled the view sequence (<Esc> key)
  287.       FALSE otherwise
  288. */
  289. BOOL ViewPicture(struct Picture *pic, struct Window *win, struct Screen *scr, int x, int y)
  290. {
  291.     int width=0, height=0;
  292.  
  293.  
  294.     width=pic->bmhd.bmh_Width;
  295.     height=pic->bmhd.bmh_Height;
  296.  
  297.     if(width+x > win->Width-18) width=(win->Width-18)-x;
  298.     if(height+y > win->Height-scr->WBorBottom) height=win->Height-(scr->WBorBottom)-y;
  299.  
  300.     if(width < 1 || height < 1) return(0);
  301.  
  302.  
  303.    BltBitMapRastPort(pic->bmap, 0,0, win->RPort, x/*+win->LeftEdge*/,y/*+win->TopEdge*/, width, height, 0xC0/*, 0xFF, NULL*/);
  304.    WaitBlit();
  305.    return(1);
  306. }
  307.  
  308. BOOL loadpic(char *fname, struct Screen *scr) {
  309.  
  310.     UBYTE pic_type[80];
  311.     LONG err;
  312.     BOOL flag=FALSE;
  313.     
  314.     if(!strcmp(fname,""))
  315.         return(0);
  316.  
  317.     DataTypesBase=OpenLibrary("datatypes.library",0);
  318.         if(!DataTypesBase) {
  319.             msg("Sorry I am unable to open datatypes.library!");
  320.             return(0);
  321.         }
  322.  
  323.         ap=AllocMem(sizeof(struct AnchorPath) + 240, MEMF_CLEAR);
  324.         if(!ap) {
  325.             msg("Unable to allocate memory for backdrop!");
  326.             CloseLibrary(DataTypesBase);
  327.             return(0);
  328.         }
  329.  
  330.         ap->ap_Strlen=240;
  331.  
  332.         for (err=MatchFirst(fname,ap); err==0; err=MatchNext(ap)) {
  333.             if(IsDataTypes(ap->ap_Buf, pic_type, 80)) {
  334.                 if(GetDataTypesPicture(ap->ap_Buf, &piccy, 0, scr) == 0) 
  335.                     flag=TRUE;
  336.             }
  337.         }
  338.     MatchEnd(ap);
  339.     FreeMem(ap,sizeof(struct AnchorPath) + 240);
  340.  
  341.     CloseLibrary(DataTypesBase);
  342.  
  343.     return(flag);
  344.  
  345. }
  346.  
  347. void pattern(struct Window *win_p, struct Screen *scr,struct Picture pic) {
  348.     int i=0, hcount=0, vcount=0 ,p=0;
  349.  
  350.     if(!win_p) {
  351.             msg("Error loading backdrop (Debug: No win_p)");
  352.             return;
  353.         }
  354.  
  355.     hcount=win_p->Width-(scr->WBorLeft+18);
  356.     hcount=hcount/pic.bmhd.bmh_Width;
  357.     hcount++;
  358.     vcount=win_p->Height-(scr->BarHeight+scr->WBorBottom+1);
  359.     vcount=vcount/pic.bmhd.bmh_Height;
  360.     vcount++;
  361.     for(p=0; p<vcount; p++) {
  362.         for(i=0; i<hcount; i++) {
  363.                 ViewPicture(&pic, win_p, scr, scr->WBorLeft+(i*pic.bmhd.bmh_Width), (scr->BarHeight+1)+(p*pic.bmhd.bmh_Height));
  364.         }
  365.     }
  366.     
  367. }
  368.  
  369. void unloadpic(struct Picture pic) {
  370.     if(!(DataTypesBase=OpenLibrary("datatypes.library",0))) {
  371.         msg("Unable to re-open datatypes library!!!");
  372.         return;
  373.     }
  374.  
  375.     FreePicture(&pic);
  376.  
  377.     CloseLibrary(DataTypesBase);
  378.  
  379. }
  380.  
  381. /* For testing only */
  382.  
  383. /*
  384. void main(int argc, char *argv[]) {
  385.     struct Window *win_p;
  386.     struct Screen *scr;
  387.     struct IntuiMessage *msg2;
  388.     ULONG class;
  389.     
  390.     scr=LockPubScreen(NULL);
  391.     if(!scr) {
  392.         msg("Unable to lock WB!");
  393.         exit(NULL);
  394.     }
  395.     win_p=(struct Window *)OpenWindowTags(NULL,    WA_Left,50,
  396.                                                 WA_Top,50,
  397.                                                 WA_Width,500,
  398.                                                 WA_Height,50,
  399.                                                 WA_MaxHeight,256,
  400.                                                 WA_MaxWidth,640,
  401.                                                 WA_MinWidth,10,
  402.                                                 WA_MinHeight,10,
  403.                                                 WA_SimpleRefresh,TRUE,
  404.                                                 WA_DragBar,TRUE,
  405.                                                 WA_CloseGadget,TRUE,
  406.                                                 WA_SizeGadget,TRUE,
  407.                                                 WA_IDCMP,IDCMP_NEWSIZE|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW,
  408.                                                 TAG_END);
  409.             
  410.     if(!win_p) {
  411.         msg("Unable to open window!");
  412.         exit(NULL);
  413.     }
  414.                             
  415.     printf("Loadpic returned %d\n",loadpic(argv[1],scr));
  416.     pattern(win_p,scr,piccy);
  417.  
  418.     while(1) {
  419.         Wait(1<<win_p->UserPort->mp_SigBit);
  420.         msg2=(struct IntuiMessage *)GetMsg(win_p->UserPort);
  421.         class=msg2->Class;
  422.         ReplyMsg((struct IntuiMessage *)msg2);
  423.  
  424.         switch(class) {
  425.             case IDCMP_NEWSIZE:
  426.             case IDCMP_REFRESHWINDOW:
  427.                 pattern(win_p,scr,piccy);
  428.             break;
  429.             case IDCMP_CLOSEWINDOW:
  430.               CloseWindow(win_p);
  431.                 unloadpic(piccy);
  432.                 exit(NULL);
  433.             break;
  434.         }
  435.     }
  436. }
  437. */